package com.ethan.guava;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

/**
 * author ethan
 * create 2018-03-23 上午11:00
 * desc
 **/
public class OptionalTest {
    public static void main(String[] args){
        //常用静态方法
        //Optional.of(T)：获得一个Optional对象，其内部包含了一个非null的T数据类型实例，若T=null，则立刻报错。
        //Optional.absent()：获得一个Optional对象，其内部包含了空值
        //Optional.fromNullable(T)：将一个T的实例转换为Optional对象，T的实例可以不为空，也可以为空[Optional.fromNullable(null)，和Optional.absent()等价。

        /**
        Optional<Integer> possible = Optional.of(6);
        Optional<Integer> absentOpt=Optional.absent();
        Optional<Integer> NullableOpt=Optional.fromNullable(null);
        Optional<Integer> NoNullableOpt=Optional.fromNullable(10);

        if(possible.isPresent()){
            System.out.println("possible isPresent:"+possible.isPresent());
            System.out.println("possible value:"+possible.get());
        }

        if(absentOpt.isPresent()){
            System.out.println("absentOpt isPresent:"+absentOpt.isPresent()); ;
        }

        if(NullableOpt.isPresent()){
            System.out.println("fromNullableOpt isPresent:"+NullableOpt.isPresent()); ;
        }
        if(NoNullableOpt.isPresent()){
            System.out.println("NoNullableOpt isPresent:"+NoNullableOpt.isPresent()); ;
        }
        **/

        Optional<Integer> possible = Optional.fromNullable(5);
//        boolean result = possible.isPresent();
////        //Integer integer = possible.get();
////        Integer integer1 = possible.or(3);
////        Integer integer2 = possible.orNull();
////        Set<Integer> integerSet = possible.asSet();

//        Optional<Boolean> t = possible.transform(new Function<Integer, Boolean>() {
//            public Boolean apply(Integer input) {
//                return 100 != input ? Boolean.TRUE : Boolean.FALSE;
//            }
//        });
//        t.get();  // false

        /**
        List<Optional<Integer>> list = new ArrayList<Optional<Integer>>();
        for (int index = 10; index > 0; --index) {
            Integer t;
            if (0 == index % 2) {
                t = index;
            } else {
                t = null;
            }
            list.add(Optional.<Integer>fromNullable(t));
        }
        Iterable<Integer> it = Optional.presentInstances(list);
        it.forEach(new Consumer<Integer>() {
            public void accept(Integer integer) {
                System.err.println(integer);
            }
        });
        **/
        Optional<Person> personOptional = Optional.fromNullable(new Person(3,"ww",22));
        Optional<List<Person>> listOptional = getList(personOptional);



        String str ="";
    }

    public static Optional<List<Person>> getList(Optional<Person> personOptional){
        List<Person> list = Lists.newArrayList(new Person(1,"zs",20),new Person(2,"ls",30));
        if(personOptional.isPresent()){
            list.add(personOptional.get());
        }
        return Optional.fromNullable(list);
    }
}

@Data
@AllArgsConstructor
class  Person{
    private int id;
    private String name;
    private int age;
}
